package com.glory.dgcpcpda.activity.recipe;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.check.CheckDescActivity;
import com.glory.dgcpcpda.activity.mfg.UpdateIPActivity;
import com.glory.dgcpcpda.adapter.TempRecipeListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.SheetInfoEntity;
import com.glory.dgcpcpda.entity.TempRecipeEntity;
import com.glory.dgcpcpda.entity.UpdateIPEntity;
import com.glory.dgcpcpda.entity.UploadData;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: TempRecipeActivity
 * Author: XJH
 * Date: 2021/4/9 10:34
 * Description:
 * History:
 */
public class UploadRecipeActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvPosition)
    TextView tvPosition;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvProgramType)
    TextView tvProgramType;
    @BindView(R.id.flRecipeID)
    FrameLayout flRecipeID;
    @BindView(R.id.tvRecipeID)
    TextView tvRecipeID;
    @BindView(R.id.ivRecipeID)
    ImageView ivRecipeID;


    private FreeUI_GeneralPop mStepPop;
    private ArrayList<String> recipeList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_upload;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("Recipe上传");
        tvBaseSubmit.setVisibility(View.GONE);

        initEdit(etEqpID, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {

                FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                        (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            UpdateIPEntity body = response.body().getResponse().getBody();
                            tvEqpType.setText(body.getEQPTYPE());
                            tvStep.setText(body.getPROCESSNAME());
                            tvPosition.setText(body.getPOSITION());
                            tvProgramType.setText(body.getPROGRAM_TYPE());
                            getData(s);
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "GETEQPINFOBYEQPID");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("EQPID",s);
                NetUtils.requestPostNet("EAP.GETEQPINFOBYEQPID","GETEQPINFOBYEQPID", UploadRecipeActivity.this,map,callBack);
            }
        });

    }

    private void getData(String s) {
        recipeList.clear();
        FreeUI_EntityCallBack<BaseResponseBean<UploadData>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<UploadData>>
                (new TypeToken<BaseResponseBean<UploadData>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<UploadData>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    recipeList.addAll(response.body().getResponse().getBody().getList());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<UploadData>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEqpRecipeList");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", s);
        NetUtils.requestPostNet("CPC.GetRecipeList", "GetRecipeList", this, map, callBack);

    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.tvUpLoad,R.id.flRecipeID})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvUpLoad:
                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<Object>>
                                (new TypeToken<BaseResponseBean<Object>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<Object>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    showToast("上传成功",true);
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<Object>> response) {
                                super.onError(response);
                                loadError(response.getException(), "uploadRecipe");
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("EqpID",getText(etEqpID));
                        map.addProperty("EqpType",tvEqpType.getText().toString());
                        map.addProperty("RecipeID",tvRecipeID.getText().toString().replace("\\","\\\\"));
                        NetUtils.requestPostNet("uploadRecipe","uploadRecipe",this,map,callBack);
                    }
                });
                break;
            case R.id.flRecipeID:
                if (mStepPop != null) {
                    if (mStepPop.isShowing()) {
                        mStepPop.dismiss();
                    }
                } else {
                    mStepPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flRecipeID.getWidth(), recipeList, ivRecipeID, (codeS, i) -> {
                        tvRecipeID.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivRecipeID);
                mStepPop.showAsDropDown(flRecipeID, 0, 0);
                break;
        }
    }
}
